package com.eh.excel;

import com.google.common.collect.Maps;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.Properties;

/**
 * Created by David Li on 2016/11/24.
 */
public final class PropertiesReadUtil {
    private static Properties properties;

    static {
        properties = new Properties();
        InputStream in = Object.class.getResourceAsStream("/excel.r.properties");
        try {
            // 解决中文乱码，使用BufferedReader包装
            properties.load(new BufferedReader(new InputStreamReader(in)));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Map<Integer, String> getSheets(String key) {
        Map<Integer, String> map = Maps.newHashMap();
        String str = properties.getProperty(key + ".sheets").trim();
        String[] sheets = str.split(",");
        for (String s : sheets) {
            String[] t = s.split(":");
            map.put(Integer.parseInt(t[0]), t[1]);
        }
        return map;
    }

    public static Map<Integer, String> getColumns(String key) {
        Map<Integer, String> map = Maps.newHashMap();
        String str = properties.getProperty(key + ".columns").trim();
        String[] headers = str.split(",");
        for (String s : headers) {
            String[] t = s.split(":");
            map.put(Integer.parseInt(t[0]), t[1]);
        }
        return map;
    }
}
